package com.ray.depot.service.impl;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.ray.depot.service.StockChangeService;
import com.ray.depot.table.customMapper.SelectStockChangeMapper;
import com.ray.depot.table.dto.StockChangeDto;
import com.ray.depot.table.entity.StockChange;
import com.ray.depot.table.entity.StockChangeExample;
import com.ray.depot.table.entity.StockChangeExample.Criteria;
import com.ray.depot.table.mapper.StockChangeMapper;
import com.zjhc.flyhong.common.function.enums.DeleteEnum;
import com.zjhc.flyhong.common.function.exception.BussinessException;
import com.zjhc.flyhong.common.function.operator.LoginUser;
import com.zjhc.flyhong.common.function.operator.PageDto;
import com.zjhc.flyhong.common.util.UUIDUtil;
import com.zjhc.flyhong.common.web.ErrorMsg;
import com.zjhc.flyhong.common.web.Result;
import com.zjhc.flyhong.common.web.SuccessMsg;
import com.zjhc.flyhong.common.web.base.BaseService;
@Service
public class StockChangeServiceImpl extends BaseService implements StockChangeService{
	/**日志对象**/
	private static Logger logger = Logger.getLogger(StockChangeServiceImpl.class);
	
	@Autowired
	private StockChangeMapper stockChangeMapper;
	
	@Autowired
	private SelectStockChangeMapper selectStockChangeMapper;

	@Transactional
	public Result<Object> addStockChange(StockChange params, LoginUser user) {
		Result<Object> result=null;
		result=new Result<Object>(0,SuccessMsg.OP_SUCCESS);
		params.setTenantId(user.getTenantId());
		params.setId(UUIDUtil.getUUID());
		params.setIsDelete(DeleteEnum.USE.getValue());
		this.setUserForAdd(params, user);
		int count=stockChangeMapper.insertSelective(params);
		if(count!=1){
			logger.error("StockChangeServiceImpl=>addStockChange is error");
			throw new BussinessException(-1, ErrorMsg.INSERT_ERROR);
		}
		return result;
	}
	
	
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public StockChange queryStockChange(String id, LoginUser user) {
		return stockChangeMapper.selectByPrimaryKey(id);
		}
	
	
    @Transactional
	public Result<Object> updateStockChange(StockChange params, LoginUser user) {
		Result<Object> result=null;
		result=new Result<Object>(0,SuccessMsg.OP_SUCCESS);
		this.setUserForUpdate(params, user);
		int count=stockChangeMapper.updateByPrimaryKeySelective(params);
		if(count!=1){
			logger.error("StockChangeServiceImpl=>editStockChange is error");
			throw new BussinessException(-1, ErrorMsg.INSERT_ERROR);
		}
		return result;
	}
    
    @Transactional
	public Result<Object> deleteStockChange(String id, LoginUser user) {
		Result<Object> result=null;
		result=new Result<Object>(0,SuccessMsg.OP_SUCCESS);
		StockChange params=new StockChange();
		params.setId(id);
		params.setIsDelete(DeleteEnum.DELETE.getValue());
		this.setUserForUpdate(params, user);
		int count=stockChangeMapper.updateByPrimaryKeySelective(params);
		if(count!=1){
			logger.error("StockChangeServiceImpl=>deleteStockChange is error");
			throw new BussinessException(-1, ErrorMsg.INSERT_ERROR);
		}
		return result;
	}
    
    @Transactional(propagation=Propagation.NOT_SUPPORTED)
	public PageDto<StockChange> queryStockChange(StockChange params, PageDto<StockChange> page, LoginUser user) {
		StockChangeExample example=new StockChangeExample();
		Criteria criteria = example.createCriteria();
		//超级管理员能查看全部
		if(!user.isSuperManager()){
			criteria.andTenantIdEqualTo(user.getTenantId());
		}
		criteria.andIsDeleteEqualTo(DeleteEnum.USE.getValue());
		this.setExamplePage(example, page);
		page.setTotal(stockChangeMapper.countByExample(example));
		page.setRecords(stockChangeMapper.selectByExample(example));
		return page;
	}
    
    @Transactional(propagation=Propagation.NOT_SUPPORTED)
	public PageDto<StockChangeDto> querySelectStockChange(StockChangeDto params, PageDto<StockChangeDto> page, LoginUser user) {
	    params.setTenantId(user.getTenantId());
	    params.setOrderByClause(page.getOrderBy());
	    params.setLimitStart((page.getPage() - 1)  * page.getPageSize());
	    params.setLimitEnd(page.getPageSize());
	    params.setIsDelete(DeleteEnum.USE.getValue());
		page.setTotal(selectStockChangeMapper.countByExample(params));
		page.setRecords(selectStockChangeMapper.selectByExample(params));
		return page;
	}

  

	@Override
	public void deleteStockChangeByNo(String stockChangeNo, LoginUser user) {
		StockChange params=new StockChange();
		params.setIsDelete(DeleteEnum.DELETE.getValue());
		this.setUserForUpdate(params, user);
		StockChangeExample example=new StockChangeExample();
		 example.createCriteria().andRecordNoEqualTo(stockChangeNo);
		 stockChangeMapper.updateByExampleSelective(params, example);
		
	}


	@Override
	public List<StockChange> queryStockChangesByStockChangeNo(String stockChangeNo, LoginUser user) {
		StockChange params = new StockChange();
		params.setRecordNo(stockChangeNo);
		params.setIsDelete(DeleteEnum.USE.getValue());
		return stockChangeMapper.selectByRecord(params);
	}

}
